package com.jacob.cloud.mall.user.controller;

import com.jacob.cloud.mall.common.common.ApiRestResponse;
import com.jacob.cloud.mall.common.common.Constant;
import com.jacob.cloud.mall.common.exception.MallException;
import com.jacob.cloud.mall.common.exception.MallExceptionEnum;
import com.jacob.cloud.mall.user.model.pojo.User;
import com.jacob.cloud.mall.user.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;

/**
 * 用户模块
 */
@RestController
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户注册
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ApiRestResponse<Object> register(
            @NotEmpty(message = "用户名不能为空") String userName,
            @NotEmpty(message = "密码不能为空")
            @Min(value = 8, message = "密码最小长度为8") String password) throws MallException {
        iUserService.register(userName, password);
        return ApiRestResponse.success();
    }

    /**
     * 用户登录
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ApiRestResponse<User> login(
            @NotEmpty(message = "用户名不能为空") String userName,
            @NotEmpty(message = "密码不能为空") String password, HttpSession session) throws MallException {
        User user = iUserService.login(userName, password);
        // 不返回用户密码
        user.setPassword(null);
        // session记录用户
        session.setAttribute(Constant.MALL_USER, user);
        return ApiRestResponse.success(user);
    }

    /**
     * 用户更新签名
     */
    @ApiOperation("用户更新签名")
    @PostMapping("/user/update")
    public ApiRestResponse<Object> updateUserInfo(String signature, HttpSession session) throws MallException {
        User currentUser = (User)session.getAttribute(Constant.MALL_USER);
        // 组织更新信息
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);

        iUserService.updateInfo(user);
        return ApiRestResponse.success();
    }

    /**
     * 用户登出
     */
    @ApiOperation("用户登出")
    @PostMapping("/user/logout")
    public ApiRestResponse<Object> logout(HttpSession session) {
        session.removeAttribute(Constant.MALL_USER);
        return ApiRestResponse.success();
    }

    /**
     * 管理员的登录
     */
    @ApiOperation("管理员的登录")
    @PostMapping("/adminLogin")
    public ApiRestResponse<User> adminLogin(
            @NotEmpty(message = "用户名不能为空") String userName,
            @NotEmpty(message = "密码不能为空") String password, HttpSession session) throws MallException {
        User user = iUserService.login(userName, password);
        // 判断是否是管理员
        if (iUserService.checkAdminRole(user)) {
            // 不返回用户密码
            user.setPassword(null);
            // session记录用户
            session.setAttribute(Constant.MALL_USER, user);
            return ApiRestResponse.success(user);
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }
    }

    /**
     * 校验是否是管理员
     * @param user
     * @return
     */
    @PostMapping("/checkAdminRole")
    @ResponseBody
    public Boolean checkAdminRole(@RequestBody User user){
        return iUserService.checkAdminRole(user);
    }

    /**
     * 获取当前登录的User对象
     * @param session
     * @return
     */
    @GetMapping("/getUser")
    public User getUser(HttpSession session){
        User currentUser = (User)session.getAttribute(Constant.MALL_USER);
        return currentUser;
    }
}
